package com.nami.logic.service.impl;

import com.nami.gate.api.request.message.DownMsgRequest;
import com.nami.gate.api.request.message.RetryMsgRequest;
import com.nami.logic.bo.send.DownModeConfig;
import com.nami.logic.event.publish.RetryDownMsgOnceEvent;
import com.nami.logic.service.RetryForwardPacketHandler;
import com.nami.logic.utils.ForwardPacketUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class RetryForwardPacketImpl implements RetryForwardPacketHandler {

    private final ApplicationEventPublisher eventPublisher;

    @Override
    public void retryHandler(RetryMsgRequest request, DownModeConfig config) throws Exception {
        switch (config.getMode()) {
            case "mq":
                eventPublisher.publishEvent(new RetryDownMsgOnceEvent(this, request));
                break;
            case "dubbo":
                log.info("[qos-pool] 【{}】开始发送消息==>{}:{}", request.getIdentityNo(), config.getDubboIp(), config.getDubboPort());
                DownMsgRequest req = new DownMsgRequest();
                req.setPacket(request.getPacket());
                req.setIdentityNo(request.getIdentityNo());
                req.setMessageId(request.getMessageId());
                ForwardPacketUtil.flushPacket(req, config.getDubboIp(), config.getDubboPort());
                break;
            default:
                log.error("[qos] 下行通信方式没找到...");

        }


    }


}
